/**
 * Thinking in java 
 *  2018/08/01
 */
package com.dzb.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import jnr.ffi.Struct.sa_family_t;
import jnr.ffi.Struct.socklen_t;
import jnr.ffi.types.in_addr_t;

/**
 * @author dzb
 * @date 2018年8月15日 上午10:25:15
 * @description  
 * @version 1.0.0
 */
public class TimeServerHandler implements Runnable {

	
	private Socket socket;
	
	
	/**
	 * 
	 */
	public TimeServerHandler(Socket socket) {
		this.socket=socket;
	}
	
	
	@Override
	public void run() {
		
		BufferedReader in=null;
		PrintWriter out = null;
		
		         try {
					in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
					out = new PrintWriter(this.socket.getOutputStream(),true);
					String cuurentTime = null;
					String body = null;
					while(true){
						body = in.readLine();
						if(body==null)
							break;
						System.out.println("this is recevie order :"+body);
						System.out.println("query time order".equalsIgnoreCase(body));
						cuurentTime = "query time order".equalsIgnoreCase(body)
								?new java.util.Date(System.currentTimeMillis()).toString():"bad order";
								out.println(cuurentTime);
					}
					
				} catch (IOException e) {
					if(in!=null){
						try {
							in.close();
						} catch (IOException e1) {
							e1.printStackTrace();
						}
					}
					if(out!=null){
						out.close();
					}
					if(this.socket!=null){
						try {
							this.socket.close();
						} catch (IOException e1) {
							e1.printStackTrace();
						}
						this.socket=null;
					}
				}
		
	}
	
	

}
